export default {
	data() {
		return {
			balance:0,
			rule: [],
			// 签到规则
			config: null,
			hasSign: 0, //今天是否签到
			signDaysSeries: 0, //连续签到次数
			timestamp: "", //当前的时间戳
			time:"",  //当前日期
			back: '', //返回页
			redirect: '', //返回方式
			// 防止重复提交
			isSubmit: false,
			selected:[],//日期选中
			effectiveBalance:0,
		};
	},
	methods: {
		navigateBack() {
			if (this.back != '') {
				this.$util.redirectTo(this.back, {}, this.redirect);
			} else {
				this.$util.redirectTo('/pages/member/index/index', {}, 'reLaunch');
			}
		},
		
		init() {
			//获取签到记录
			let year = (new Date()).getFullYear();
			let month = (new Date()).getMonth() + 1;
			this.getSigninList(year, month);
			
			this.time = this.$util.timeStampTurnTime(this.timestamp).slice(0,10);
			
			this.getConfig();
			this.getIsSign();
			this.getEffectiveBalance();
		},

		/**
		 * 获取配置信息
		 */
		getConfig(){
			this.$api.sendRequest({
				url: '/membersignin/api/signin/config',
				success: res => {
					this.timestamp = res.timestamp;
					this.time = this.$util.timeStampTurnTime(this.timestamp).slice(0,10)
					
					if (res.code == 0 && res.data) {
						if(res.data.is_use){
							this.isUse = res.data.is_use
							this.config = res.data.value;
						}else{
							this.$util.showToast({
								'title':'签到功能尚未启用'
							});
							setTimeout(()=>{
								this.$util.redirectTo('/pages/member/index/index',{},'reLaunch')
							},1500)
						}
					}
				}
			});
		},
		
		/**
		 * 获取有效签到奖励余额
		 */
		getEffectiveBalance() {
			this.$api.sendRequest({
				url: '/membersignin/api/signin/getEffectiveBalance',
				success: res => {
					if (res.code >= 0) {
						this.effectiveBalance = Number(res.data);
					}
				}
			});
		},
		
		/**
		 * 判断当前是否签到
		 */
		getIsSign() {
			this.$api.sendRequest({
				url: '/api/membersignin/issign',
				success: res => {
					if (res.code == 0) {
						this.hasSign = res.data
					}
				}
			});
		},
		
		//签到
		sign() {
			if (!this.verify()) return;
			
			if (this.isSubmit) return;
			
			if (!this.hasSign) {	
				this.$api.sendRequest({
					url: '/api/membersignin/signin',
					success: res => {
						if (res.code == 0) {
							if(res.data && res.data.member_balance){
								this.balance = Number(res.data.member_balance).toFixed(2);
								this.$refs.signin.open();
								let date = new Date();
								this.getSigninList(date.getFullYear(), date.getMonth() + 1);
							}
						} else {
							this.$util.showToast({
								title: res.message
							})
						}
					}
				});
			}
		},
		
		/**
		 * 验证
		 */
		verify() {
			// 验证登录
			if (!uni.getStorageSync('token')) {
				this.$util.showToast({title: '您还未登录'});
				return false;
			}
			
			// 验证签到是否开启
			if (!this.isUse) {
				this.$util.showToast({title: '签到未启用'});
				return false;
			}
			
			// 验证是否已签到
			if (this.hasSign) {
				this.$util.showToast({title: '您已签到'});
				return false;
			}
			
			// 验证是否限制当前会员的有效余额
			if (this.config && this.config.limit.min_balance > 0 && this.config.limit.min_balance > this.effectiveBalance) {
				this.$util.showToast({title: '签到所需最小有效余额为' + this.config.limit.min_balance});
				return false;
			}
			
			return true;
		},
		
		/**
		 * 日期改变触发
		 * @param {Object} res
		 */
		change(res) {
			console.log('日期改变触发', res)
		},
		
		/**
		 * 切换月份时触发
		 * @param {Object} res
		 */
		monthSwitch(res) {
			console.log('切换月份时触发', res)
			this.getSigninList(res.year, res.month);
		},
		
		/**
		 * 获取签到记录
		 * @param {Object} year
		 * @param {Object} month
		 */
		getSigninList(year, month){
			this.$api.sendRequest({
				url: '/membersignin/api/Signin/signInList',
				data:{
					year : year,
					month : month,
				},
				success: res => {
					if(res.code >= 0){
						let data = res.data;
						let selected = [];
						for(let i in data){
							let item = data[i];
							let day = (new Date(item.create_time * 1000)).getDate();
							if(month < 10) month = '0' + month;
							if(day < 10) day = '0' + day;
							let date = year + '-' + month + '-' + day;
							
							selected.push({
								date : date,
								info : item.account_data,
							})
							console.log(123);
							console.log(selected);
							this.selected = selected;
							console.log(selected);
						}
					}
				}
			});
		}
	
		,closeMask(){//关闭弹窗
			this.isMask=false;
		},
		// update(){
		// 	console.log('更新操作');
		// }
		
		
	
	
	}
};
